package com.yq.netty.nio.groupChat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * @ClassName GroupChatServer
 * @Description 群聊服务端
 * @Author yaoqiang
 * @Date 2021/3/25 9:37
 * @Version 1.0
 */
public class GroupChatServer {
    private Selector selector;
    private ServerSocketChannel listenerChannel;
    private final static int PORT = 6667;


    //构造器，做初始化工作
    public GroupChatServer() {
        try {
            //得到选择器
            selector = Selector.open();
            //serversocketChannel
            listenerChannel = ServerSocketChannel.open();
            //绑定端口
            listenerChannel.socket().bind(new InetSocketAddress(PORT));
            //设置非阻塞模式
            listenerChannel.configureBlocking(false);
            //将listener注册到selector
            listenerChannel.register(selector, SelectionKey.OP_ACCEPT);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {

    }

    /**
     * 监听服务
     */
    public void listen() {
        try {
            int count=selector.select(2000);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }
    }
}
